<?php

namespace App\Http\Controllers\Home;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest',[
            'except' =>['logout']

        ]);
    }
    public function login(){
        return view('home.index.login');
    }

    public function store(Request $request){
//        dd($request->all());
        $request->validate([
            'account'=>'required',
            'password'=>'required|min:6'
        ],[
            'account.required'=>'账号不能为空',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码不能少于6位'
        ]);

        if(filter_var($request->account,FILTER_VALIDATE_EMAIL)){
            $data['email']= $request->account;
        }else{
            $data['phone']= $request->account;
        }
        $data['password']= $request->password;
        if(\Auth::attempt($data,$request->remember)){
            return redirect('/')->with('success','登陆成功');
        }
        return back()->with('error','用户名或密码不正确');
    }

    public function logout(){

        \Auth::logout();
        return redirect('/')->with('success','退出登陆');
    }
}
